﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Parameters;

namespace QLNSClient
{
    public partial class uc_ThongkeKT : UserControl
    {
        QLNSServiceReference.QLNSServiceClient sv = new QLNSServiceReference.QLNSServiceClient();
        
        public uc_ThongkeKT()
        {
            InitializeComponent();
        }

        private void gv_thongkeKT_Click(object sender, EventArgs e)
        {

        }  


        public void LoadData()
        {
            grc_Chitiet.Visible = false;
                    
            t = DateTime.Today;

            int namdau = 2009;
            int namcuoi = 2014;
            int t1 = Int32.Parse(t.Year.ToString());
            if ((t1 - namcuoi) > 0)
            {
                namdau = namdau + 1;
                namcuoi = namcuoi + 1;
            }
        

            for (int i = namdau; i <= namcuoi; i++)
            {
                cmb_Nam.Items.Add(i);
            }

            int tempT = DateTime.Today.Year;
            cmb_Nam.Text = tempT.ToString();

            gv_thongkeKT.DataSource = sv.getThongketheoNam(t);
            rdg_thongkeKT.SelectedIndex = 0;

            lke_DMDV.Properties.DataSource = sv.getDMDV();
            lke_DMDV.Enabled = false;
            
            //DateTime d=DateTime.Today;
            //d.Year.ToString();
           

            

        }

        private void uc_ThongkeKT_Load(object sender, EventArgs e)
        {
            LoadData();
        }



        int index;
        private void rdg_thongkeKT_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            index = Int32.Parse(rdg_thongkeKT.EditValue.ToString());
            if (rdg_thongkeKT.SelectedIndex != 0 && lke_DMDV.EditValue.ToString()=="")
            {
                gv_thongkeKT.DataSource = sv.getThongKeKT(index, t);
                lbl_TS.Text = sv.countSLKT(index, t).ToString();
             }
            if(rdg_thongkeKT.SelectedIndex == 0 && lke_DMDV.EditValue.ToString()=="")
            {
                gv_thongkeKT.DataSource = sv.getThongketheoNam(t);
                lbl_TS.Text = sv.countKTtheoNam(t).ToString();
            }
            if(rdg_thongkeKT.SelectedIndex != 0 && lke_DMDV.EditValue.ToString()!="")
            {
                gv_thongkeKT.DataSource=sv.getTKKTtheoNamDVMaKT(index, a, t);
                lbl_TS.Text = sv.countKTtheoNamDVMaKT(index, a, t).ToString();
            }
            if (rdg_thongkeKT.SelectedIndex == 0 && lke_DMDV.EditValue.ToString() != "")
            {
                gv_thongkeKT.DataSource = sv.getTKKTtheoNamDV(a,t);
                lbl_TS.Text = sv.countKTtheoNamDV(a, t).ToString();
            }
        }
        

        DateTime t;
        private void cmb_Nam_SelectedIndexChanged(object sender, EventArgs e)
        {
            string a = cmb_Nam.Text;
            t = new DateTime();
            t = DateTime.ParseExact(a, "yyyy", null);
            gv_thongkeKT.DataSource = sv.getThongketheoNam(t);
            rdg_thongkeKT.SelectedIndex = 0;

            grc_Chitiet.Visible = true;

            lbl_LDTT.Text = sv.countLDTT(t).ToString();
            lbl_TDCS.Text = sv.countTDCS(t).ToString();
            lbl_TDCB.Text = sv.countTDCB(t).ToString();
            lbl_TDTQ.Text = sv.countTDTQ(t).ToString();

            if (sv.countKTtheoNam(t) != 0)
            {
                lbl_TS.Text = sv.countKTtheoNam(t).ToString();
            }
            else
            {
                lbl_TS.Text = "Không có dữ liệu!";
            }
            
        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }



        private void btn_rptKT_Click(object sender, EventArgs e)
        {
            


            if (rdg_thongkeKT.SelectedIndex == 0)
                {

                    ktkt rptTonghopKT = new ktkt();
                    //rpt_ChitietKT rptTonghopKT = new rpt_ChitietKT();
                    
                    using(ReportPrintTool printTool=new ReportPrintTool(rptTonghopKT))
                    {
                        Parameter nam = new Parameter();
                        nam.Name = "Choosed";
                        nam.Type = typeof(System.Int32); 
                        nam.Value = t.Year;
                        rptTonghopKT.Parameters.Add(nam);
                        nam.Visible = false;

                        rptTonghopKT.FilterString = "[Nam]=?Choosed";
                        rptTonghopKT.ShowRibbonPreviewDialog();
                    }
                    
                }
            else
                {
                    rpt_All_KT rptAllKT = new rpt_All_KT();
                    using (ReportPrintTool printTool = new ReportPrintTool(rptAllKT))
                    {
                            rptAllKT.DataSource = sv.getReportLoaiKT(index, t);
                    }
                    rptAllKT.ShowRibbonPreviewDialog();
                }           
            
        }

        string a;
        private void lke_DMDV_EditValueChanged(object sender, EventArgs e)
        {

            rdg_thongkeKT.SelectedIndex = 0;
            a = lke_DMDV.EditValue.ToString();
            gv_thongkeKT.DataSource = sv.getTKKTtheoNamDV(a,t);
            lbl_TS.Text = sv.countKTtheoNamDV(a,t).ToString();
            lbl_LDTT.Text = sv.countLDTTtheoNamDV(a, t).ToString();
            lbl_TDCS.Text = sv.countTDCStheoNamDV(a, t).ToString();
            lbl_TDCB.Text = sv.countTDCBtheoNamDV(a, t).ToString();
            lbl_TDTQ.Text = sv.countTDTQtheoNamDV(a,t).ToString();

        }

        private void ck_DMDV_CheckedChanged(object sender, EventArgs e)
        {
            if (ck_DMDV.Checked == true)
            {
                rdg_thongkeKT.SelectedIndex = 0;
                gv_thongkeKT.DataSource = sv.getThongketheoNam(t);
                lke_DMDV.Enabled = true;
                lke_DMDV.Properties.DataSource = sv.getDMDV();
                lbl_TS.Text = sv.countKTtheoNam(t).ToString();
                lbl_LDTT.Text = sv.countLDTT(t).ToString();
                lbl_TDCS.Text = sv.countTDCS(t).ToString();
                lbl_TDCB.Text = sv.countTDCB(t).ToString();
                lbl_TDTQ.Text = sv.countTDTQ(t).ToString();
                
            }
            else
            {
                lke_DMDV.Enabled = false;
                lke_DMDV.EditValue = "";
                gv_thongkeKT.DataSource = sv.getThongketheoNam(t);
                rdg_thongkeKT.SelectedIndex = 0;
                lbl_TS.Text = sv.countKTtheoNam(t).ToString();
                lbl_LDTT.Text = sv.countLDTT(t).ToString();
                lbl_TDCS.Text = sv.countTDCS(t).ToString();
                lbl_TDCB.Text = sv.countTDCB(t).ToString();
                lbl_TDTQ.Text = sv.countTDTQ(t).ToString();
            }
        }

        
    }
}
